﻿using System;
using nRoute.Components;
using nRoute.Navigation;

namespace nRoute.ViewModels
{
    public abstract class NavigationViewModelBase
         : ViewModelBase, ISupportNavigationLifecycle, ISupportNavigationState
    {

#region ISupportNavigationLifecycle Members

        void ISupportNavigationLifecycle.Initialize(ParametersCollection requestParameters)
        {
            OnInitialize(requestParameters);
        }

        void ISupportNavigationLifecycle.Closing(Action<bool> confirmCallback)
        {
            OnClosing(confirmCallback);
        }

#endregion

#region INavigationStateManager Members

        void ISupportNavigationState.RestoreState(ParametersCollection state)
        {
            OnRestoreState(state);
        }

        ParametersCollection ISupportNavigationState.SaveState()
        {
            return OnSaveState();
        }

#endregion

#region Overridables

        protected virtual void OnInitialize(ParametersCollection state) { }

        protected virtual void OnClosing(Action<bool> confirmCallback)
        {
            if (confirmCallback != null) confirmCallback(true);     // by default always true
        }

        protected virtual void OnRestoreState(ParametersCollection state) { }

        protected virtual ParametersCollection OnSaveState()
        {
            return null;
        }

#endregion

    }
}
